package fr.zadoka.zadokadna.db;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.support.v4.util.SparseArrayCompat;

public class FavoriteProvider extends ContentProvider {
	
	// Indicates an invalid content URI
    public static final int INVALID_URI = -1;
    
    // Indicates that the incoming query is for all favorite
    public static final int FAVORITE_ZADOKA_QUERY = 1;
       
	private static final UriMatcher sUriMatcher;
	private static final SparseArrayCompat<String> sMimeTypes;
	private ZadokaDatabase mDatabase;    
	
    static {
        
        // Creates an object that associates content URIs with numeric codes
        sUriMatcher = new UriMatcher(0);

        /*
         * Sets up an array that maps content URIs to MIME types, via a mapping between the
         * URIs and an integer code. These are custom MIME types that apply to tables and rows
         * in this particular provider.
         */
        sMimeTypes = new SparseArrayCompat<String>();

        // Adds a URI "match" entry that maps Favorite content URIs to a numeric code
        sUriMatcher.addURI(
        		ZadokaDatabaseContract.AUTHORITY,
        		ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_TABLE_NAME,
        		FAVORITE_ZADOKA_QUERY);

        
        // Specifies a custom MIME type for the picture URL table
        sMimeTypes.put(
        		FAVORITE_ZADOKA_QUERY,
                "vnd.android.cursor.dir/vnd." +
                ZadokaDatabaseContract.AUTHORITY + "." +
                ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_TABLE_NAME);
    }


	@Override
	public int delete(Uri uri, String whereClause, String[] whereArgs) {
		
		
        switch (sUriMatcher.match(uri)) {

	        case FAVORITE_ZADOKA_QUERY:
	            // Updates the table
	            int rows = mDatabase.deleteFavorite(whereClause, whereArgs);
	
	            // If the update succeeded, notify a change and return the number of updated rows.
	            if (0 != rows) 
	                getContext().getContentResolver().notifyChange(uri, null);
	            return rows;
	
	        case INVALID_URI:
	    		throw new IllegalArgumentException("Query -- Invalid URI:" + uri);
        }
        return 0;
    }

	@Override
	public String getType(Uri arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
        switch (sUriMatcher.match(uri)) {

        case FAVORITE_ZADOKA_QUERY:
        		long id = mDatabase.insertFavorite(values);
        		if (-1 != id) {
                    getContext().getContentResolver().notifyChange(uri, null);
                    return Uri.withAppendedPath(uri, Long.toString(id));
                } else {

                    throw new SQLiteException("Insert error:" + uri);
                }
        case INVALID_URI:

            throw new IllegalArgumentException("Query -- Invalid URI:" + uri);
    }

    return null;	
	}

	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		mDatabase = new ZadokaDatabase(getContext());
		mDatabase.open();
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
			String sortOrder) {
        // Decodes the content URI and maps it to a code
        switch (sUriMatcher.match(uri)) {

            case FAVORITE_ZADOKA_QUERY:
                Cursor returnCursor = mDatabase.getFavoriteCursor(projection, selection, selectionArgs, sortOrder);
                // Sets the ContentResolver to watch this content URI for data changes
                returnCursor.setNotificationUri(getContext().getContentResolver(), uri);
                return returnCursor;

            case INVALID_URI:

                throw new IllegalArgumentException("Query -- Invalid URI:" + uri);
        }

        return null;	
    }

	@Override
	public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	

}
